
import pygame
import math

# 初始化Pygame
pygame.init()

# 游戏窗口设置
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("足球射门游戏")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# 足球属性
ball_radius = 15
ball_color = GREEN
ball_pos = [WIDTH//4, HEIGHT-50]
ball_speed = [0, 0]
dragging = False
click_offset = (0, 0)

# 球门属性
goal_width = 100
goal_height = 200
goal_pos = (WIDTH - 150, HEIGHT//2 - goal_height//2)

# 物理参数
gravity = 0.5
friction = 0.99
bounce = 0.7

# 得分
score = 0

def draw_goal():
    """绘制球门"""
    # 球门框
    pygame.draw.rect(screen, BLUE, (goal_pos[0], goal_pos[1], 10, goal_height))  # 左门柱
    pygame.draw.rect(screen, BLUE, (goal_pos[0]+goal_width-10, goal_pos[1], 10, goal_height))  # 右门柱
    pygame.draw.rect(screen, BLUE, (goal_pos[0], goal_pos[1], goal_width, 10))
  # 横梁

def check_goal():
    """检测是否得分"""
    global score
    # 球门得分区域（稍微放宽条件）
    goal_rect = pygame.Rect(goal_pos[0]+10, goal_pos[1]+10,
                          goal_width-20, goal_height-20)
    # 检测足球中心是否进入球门区域
    ball_center = (int(ball_pos[0]), int(ball_pos[1]))
    if goal_rect.collidepoint(ball_center):
        score += 1
        return True
    return False

def reset_ball():
    """重置足球位置"""
    global ball_pos, ball_speed
    ball_pos = [WIDTH//4, HEIGHT-50]
    ball_speed = [0, 0]

# 游戏主循环
running = True
while running:
    screen.fill(WHITE)

    # 事件处理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # 检测是否点击到足球
            mouse_pos = pygame.mouse.get_pos()
            distance = math.hypot(mouse_pos[0]-ball_pos[0], mouse_pos[1]-ball_pos[1])
            if distance <= ball_radius:
                dragging = True
                click_offset = (ball_pos[0] - mouse_pos[0], ball_pos[1] - mouse_pos[1])
        elif event.type == pygame.MOUSEBUTTONUP and dragging:
            dragging = False
            # 计算发射速度（根据拖拽距离）
            drag_distance = (ball_pos[0] - (click_offset[0] + event.pos[0]),
                            ball_pos[1] - (click_offset[1] + event.pos[1]))
            ball_speed = [drag_distance[0] * 0.15, drag_distance[1] * 0.15]
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                reset_ball()

    # 拖拽足球
    if dragging:
        mouse_pos = pygame.mouse.get_pos()
        ball_pos[0] = mouse_pos[0] + click_offset[0]
        ball_pos[1] = mouse_pos[1] + click_offset[1]

    # 物理运动
    if not dragging:
        # 应用重力
        ball_speed[1] += gravity

        # 更新位置
        ball_pos[0] += ball_speed[0]
        ball_pos[1] += ball_speed[1]

        # 边界碰撞检测
        if ball_pos[0] <= ball_radius or ball_pos[0] >= WIDTH - ball_radius:
            ball_speed[0] *= -bounce
            ball_pos[0] = max(ball_radius, min(ball_pos[0], WIDTH - ball_radius))
        if ball_pos[1] >= HEIGHT - ball_radius:
            ball_speed[1] *= -bounce
            ball_pos[1] = HEIGHT - ball_radius
            ball_speed[0] *= friction  # 地面摩擦

        # 速度衰减
        ball_speed[0] *= friction
        ball_speed[1] *= friction

    # 检测得分
    if check_goal():
        reset_ball()

    # 绘制元素
    draw_goal()
    pygame.draw.circle(screen, ball_color, (int(ball_pos[0]), int(ball_pos[1])), ball_radius)

    # 显示得分
    font = pygame.font.Font(None, 36)
    text = font.render(f"Score: {score}", True, BLACK)
    screen.blit(text, (10, 10))

    pygame.display.flip()
    pygame.time.Clock().tick(60)

pygame.quit()

#
#游戏功能说明：
#1. 鼠标拖拽足球进行射门，拖拽距离和方向决定射门力度和角度
#2. 足球具有物理效果（重力、空气阻力、地面摩擦）
#3. 蓝色区域为球门，足球进入球门区域得分
#4. 按空格键可以重置足球位置
#5. 左上角显示当前得分
#6. 足球碰撞边界会有反弹效果
#
#操作指南：
#1. 点击并拖拽足球来调整射门方向和力度
#2. 松开鼠标左键发射足球
#3. 尝试让足球进入蓝色球门区域得分
#4. 按空格键随时重置足球位置
#
#游戏使用简单的物理模拟，包括：
#- 重力加速度
#- 空气阻力（速度衰减）
#- 地面摩擦
#- 碰撞反弹
#- 抛物线运动
#
#你可以通过调整代码中的物理参数（gravity、friction、bounce）来改变游戏的运动特 性，使其更容易或更难控制。